<?php

namespace App\AbstractFactory;

use App\AbstractFactory\UnitAbstract;

abstract class CompositeUnitAbstract extends UnitAbstract
{
    private $units = [];

    public function getComposite()
    {
        return $this;
    }

    protected function units()
    {
        return $this->units;
    }

    public function removeUnit(UnitInterface $unit)
    {
        $this->units = array_udiff($this->units, [$unit], function ($a, $b) {
            return $a === $b ? false : 1;
        });
    }

    public function addUnit(UnitInterface $unit)
    {
        if (in_array($unit, $this->units, true)) {
            return true;
        }
        $this->units[] = $unit;
    }
}
